package com.neusoft.liduan.d0923;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;

import org.junit.Test;

public class 网络编程 {
	/*	网络编程
	 * 		网络编程从大的方面说就是对信息的发送到接收，中间传输为物理线路的作用。
	 * 	网络通信协议
	 * 		TCP：三次握手原则，长连接，资源消耗比较大，代表的Socket。数据传输100%会到达。C/S应用
	 * 		UDP：只管发，不确保能不能收到。C/S应用
	 * 		HTTP：超文本传输协议，主要是浏览器。B/S应用
	 * 		FTP：文本文件传输
	 * 	相关类
	 * 		InetAddress：与本机的网络信息有关的类
	 * 			InetAddress getLocalHost()：获取本地主机
	 * 			InetAddress getByName(String host) ：通过ip地址获取InetAddress
	 * 			getCanonicalHostName() ：获取计算机名称
	 * 			getHostAddress() :获取IP地址
	 * 
	 * 		ServerSocket：服务端类
	 * 		ServerSocket(int port) 监听端口
	 */
	
	public static void main(String[] args) {
		t1();
	}
	
	public static void t1() {
		try {
			InetAddress ia = InetAddress.getLocalHost();
			System.out.println(ia);
			System.out.println(InetAddress.getByName("192.168.1.39").getCanonicalHostName());
			System.out.println(ia.getCanonicalHostName());
			System.out.println(ia.getLoopbackAddress());//localhost/127.0.0.1
			
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}
	
	//服务器
	@Test//不能是static方法
	public void t2Server() {
		try {
			System.out.println("启动服务端");
			ServerSocket server = new ServerSocket(9527);
			Socket accept = server.accept();//等socket客户端连接
//			System.out.println(accept);
			InputStream is = accept.getInputStream();
			byte[] b = new byte[1024];
			is.read(b);
			System.out.println(new String(b));
			OutputStream os = accept.getOutputStream();
			os.write("name:che,x:100,y:100".getBytes());
			System.out.println("服务端关闭");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void t2Client() {
		try {
			Socket s = new Socket(InetAddress.getLocalHost(), 9527);
			OutputStream os = s.getOutputStream();
			os.write("这是客户端信息".getBytes());
			InputStream is = s.getInputStream();
			int size = is.available();
			byte[] b = new byte[size];
			is.read(b);
			System.out.println(new String(b));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//完成，基于Socket的聊天室，输入byebye关闭
	//完成，群聊

	@Test
	public void t3Server() {
		try {
			System.out.println("服务端开启");
			DatagramSocket s = new DatagramSocket(8888);//做接收方才需要端口号
			//DatagramPacket数据包类
			byte[] b = new byte[20];
			DatagramPacket p = new DatagramPacket(b, 10);
			s.receive(p);
			System.out.println(new String(p.getData()));
			p.setData("收到了".getBytes());
			p.setAddress(InetAddress.getByName("192.168.1.138"));
			p.setPort(8080);
			s.send(p);
			System.out.println("服务端关闭");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void t3Client() {
		try {
			DatagramSocket s = new DatagramSocket(8080);//作为发送不需要端口号
			//DatagramPacket数据包类
//			byte[] b = new byte[20];
			DatagramPacket p = new DatagramPacket("haha".getBytes(), "haha".length(), InetAddress.getByName("192.168.1.138"), 8888);
			s.send(p);
			System.out.println("发送完毕");
			byte[] b = new byte[20];
			p = new DatagramPacket(b, 10);
			s.receive(p);
			System.out.println(new String(p.getData()));
//			DatagramSocket s = new DatagramSocket(8080);//做接收方才需要端口号
//			byte[] b = new byte[20];
//			DatagramPacket p = new DatagramPacket(b, 10);
//			s.receive(p);
//			p.setData("收到了".getBytes());
//			p.setAddress(InetAddress.getByName("192.168.1.137"));
//			p.setPort(8080);
//			s.send(p);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
